/*
* Copyright (C) 2012-2014 qar
* License: http://www.gnu.org/licenses/gpl.html GPL version 2
*/

#ifndef HASHER_H
#define HASHER_H

#include <QtCore>

class HashFunction;

class Hasher
{
public:
    enum IntegrityAlgorithm {
        NoIntegrity = 0,
        Md5 = 1,
        Sha1 = 2
    };

    Hasher(IntegrityAlgorithm algorithm);
    virtual ~Hasher();

    void setAlgorithm(IntegrityAlgorithm algorithm);
    IntegrityAlgorithm getAlgorithm() const;
    QString getAlgorithmName() const;

    qint64 hashSize() const;

    // Online hash functions
    void reset();
    void addData(const QByteArray &data);
    void addData(const char *data, int length);
    QByteArray result() const;

    // Offline hash functions
    QByteArray offlineHash(const QByteArray &data) const;
    static QByteArray offlineHash(const QByteArray &data, IntegrityAlgorithm algorithm);

    // Serialization of parameters
    bool toStream(QDataStream &s);
    bool fromStream(QDataStream &s);

protected:
    IntegrityAlgorithm algorithm;
    HashFunction *hashFunction;

private:
    Q_DISABLE_COPY(Hasher)
};

#endif // HASHER_H
